﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using ZWFrameWork;

/// <summary>
/// 登录服务器消息处理
/// </summary>
public class LogonNetMessageCenter
{
    public void NetMessageDispatch(TCP_Command command, byte[] mainMsg)
    {
        
        if (command.wMainCmdID == Protocal.MDM_MB_LOGON || command.wMainCmdID == Protocal.MDM_GP_LOGON)
        {
            MDM_MB_LOGON_HANDLER(command, mainMsg);
            return;
        }

        if (command.wMainCmdID == Protocal.MDM_MB_SERVER_LIST)
        {
            MDM_MB_SERVER_LIST_HANDLER(command, mainMsg);
            return;
        }

        ///用户服务
        if (command.wMainCmdID == Protocal.MDM_GP_USER_SERVICE)
        {
            MDM_GP_USER_SERVICE_HANDLER(command, mainMsg);
            return;
        }

        if (command.wMainCmdID == Protocal.MDM_GP_PROPERTY)
        {
            MDM_GP_PROPERTY_HANDLER(command, mainMsg);
            return;
        }

        if (command.wMainCmdID == Protocal.MDM_MB_PERSONAL_SERVICE)
        {
            MDM_MB_PERSONAL_SERVICE_HANDLER(command, mainMsg);
            return;
        }

        if (command.wMainCmdID == Protocal.MDM_GP_SERVER_LIST)
        {
            MDM_GP_SERVER_LIST_HANDLER(command, mainMsg);
            return;
        }

        GameUtilty.Error("LogonNetMessageCenter 消息号未定义{0}", command.wMainCmdID);
    }

    /// <summary>
    /// MDM_MB_PERSONAL_SERVICE_HANDLER
    /// </summary>
    /// <param name="command"></param>
    /// <param name="mainMsg"></param>
    private void MDM_MB_PERSONAL_SERVICE_HANDLER(TCP_Command command, byte[] mainMsg)
    {
        if (command.wSubCmdID == Protocal.SUB_MB_PERSONAL_GET_OFFLINE_INFO_RESULT)
        {
            CMD_GP_GetOffLineInfoResult cmd = NetUtils.bytesToStruct<CMD_GP_GetOffLineInfoResult>(mainMsg);
            if (cmd.dwServerID != 0)
            {
                LogonClient.Instance.CloseSocketAndRelease();
            }
            else
            {
            }
        }
    }


    /// <summary>
    /// MDM_GP_PROPERTY_HANDLER
    /// </summary>
    /// <param name="command"></param>
    /// <param name="mainMsg"></param>
    private void MDM_GP_PROPERTY_HANDLER(TCP_Command command, byte[] mainMsg)
    {
        if (command.wSubCmdID == Protocal.SUB_GP_QUERY_SINGLE_RESULT)
        {
            CMD_GP_PropertyQuerySingleResult cmd = NetUtils.bytesToStruct<CMD_GP_PropertyQuerySingleResult>(mainMsg);
        }
        else if (command.wSubCmdID == Protocal.SUB_GP_PROPERTY_BUY_RESULT)
        {
            CMD_GP_S_PropertySuccess cmd = NetUtils.bytesToStruct<CMD_GP_S_PropertySuccess>(mainMsg);
        }
        else if (command.wSubCmdID == Protocal.SUB_GP_PROPERTY_FAILURE)
        {
            CMD_GP_PropertyFailure cmd = NetUtils.bytesToStruct<CMD_GP_PropertyFailure>(mainMsg);
        }
        else
        {
            GameUtilty.Error("此消息未定义! wSubCmdID:{0}", command.wSubCmdID);
        }
    }


    /// <summary>
    /// MDM_GP_USER_SERVICE_HANDLER
    /// </summary>
    /// <param name="command"></param>
    /// <param name="mainMsg"></param>
    private void MDM_GP_USER_SERVICE_HANDLER(TCP_Command command, byte[] mainMsg)
    {
        if (command.wSubCmdID == Protocal.SUB_GP_USER_INDIVIDUAL) //个人资料
        {
            SingletonInstance<PlayerInfo>.Instance().OnSubQueryUserInfoResult(mainMsg);
            SingletonInstance<PlayerInfo>.Instance().SendQUERY_INSURE_INFO();
            
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_INDIVIDUAL_RESULT) //完善资料 --实名认证
        {
            CMD_GP_IndividuaResult cm = NetUtils.bytesToStruct<CMD_GP_IndividuaResult>(mainMsg);
            SingletonInstance<PlayerInfo>.Instance().UpdatePlayerScore(cm);
            LogonClient.Instance.CloseSocketAndRelease();
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_USER_FACE_INFO) //完善资料
        {
            CMD_GP_UserFaceInfo cm = NetUtils.bytesToStruct<CMD_GP_UserFaceInfo>(mainMsg);
            SingletonInstance<PlayerInfo>.Instance().wFaceID = cm.wFaceID;
            LogonClient.Instance.CloseSocketAndRelease();
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_OPERATE_SUCCESS) //操作成功
        {
            CMD_GP_OperateSuccess cm = NetUtils.bytesToStruct<CMD_GP_OperateSuccess>(mainMsg);
            LogonClient.Instance.CloseSocketAndRelease();
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_OPERATE_FAILURE) //操作失败
        {
            CMD_GP_OperateFailure cm = NetUtils.bytesToStruct<CMD_GP_OperateFailure>(mainMsg);
            LogonClient.Instance.CloseSocketAndRelease();
            return;
        }

        //--------------------银行Begin=-------------------------------------
        if (command.wSubCmdID == Protocal.SUB_GP_USER_INSURE_INFO) //银行资料
        {
            CMD_GP_UserInsureInfo cm = NetUtils.bytesToStruct<CMD_GP_UserInsureInfo>(mainMsg);
            
            
            
            return;
        }


        if (command.wSubCmdID == Protocal.SUB_GP_USER_INSURE_ENABLE_RESULT) //银行开通结果
        {
            CMP_GP_UserInsureEnableResult cmd = NetUtils.bytesToStruct<CMP_GP_UserInsureEnableResult>(mainMsg);
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_USER_INSURE_SUCCESS) //--银行操作成功 存取赠
        {
            CMP_GP_UserInsureSuccess cmd = NetUtils.bytesToStruct<CMP_GP_UserInsureSuccess>(mainMsg);
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_USER_INSURE_FAILURE) //--银行操作失败 存取赠
        {
            CMP_GP_UserInsureFailure cmd = NetUtils.bytesToStruct<CMP_GP_UserInsureFailure>(mainMsg);
            return;
        }

        // -------------------------银行End-----------------------------

        // -------------------------签到Begin---------------------------
        if (command.wSubCmdID == Protocal.SUB_GP_CHECKIN_INFO) //签到信息
        {
            CMD_GP_CheckInInfo cmd = NetUtils.bytesToStruct<CMD_GP_CheckInInfo>(mainMsg);
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_CHECKIN_INFO_END) //签到礼包接收完成
        {
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_GIFTTURNPLATE_QUERRY_RESULT) //查询转盘回复
        {
            CMD_GP_TurnTableInfo cmd = NetUtils.bytesToStruct<CMD_GP_TurnTableInfo>(mainMsg);
            return;
        }

        if (command.wSubCmdID == Protocal.SUB_GP_GIFTTURNPLATE_RESULT) //抽奖回复
        {
            CMD_GP_GiftTurnPlate_StartResult cmd = NetUtils.bytesToStruct<CMD_GP_GiftTurnPlate_StartResult>(mainMsg);

            return;
        }

        //-------------------------签到End---------------------------
        if (command.wSubCmdID == Protocal.SUB_GP_GET_USER_WEALTH_RESULT)
        {
            CMD_GP_GetUserWealthResult cmd = NetUtils.bytesToStruct<CMD_GP_GetUserWealthResult>(mainMsg);
            return;
        }
    }


    /// <summary>
    /// MDM_MB_LOGON_HANDLER
    /// </summary>
    private void MDM_MB_LOGON_HANDLER(TCP_Command command, byte[] mainMsg)
    {
        if (command.wSubCmdID == Protocal.SUB_MB_LOGON_SUCCESS) //登录成功(读取用户信息)
        {
            GameUtilty.Info("登陆成功，获取信息");
            CMD_MB_LogonSuccess cmd = NetUtils.bytesToStruct<CMD_MB_LogonSuccess>(mainMsg);
            //缓存登录信息
            SingletonInstance<PlayerInfo>.Instance().UpdateLoginInfo(cmd);
        }
        else if (command.wSubCmdID == Protocal.SUB_MB_LOGON_FAILURE) //登录失败
        {
            GameUtilty.Info("登陆失败");
            CMD_MB_LogonFailure cmd = NetUtils.bytesToStruct<CMD_MB_LogonFailure>(mainMsg);
        }
        else if (command.wMainCmdID == Protocal.SUB_MB_UPDATE_SUCCESS) //升级账号成功
        {
            GameUtilty.Info("升级账号成功");
            CMD_MB_UpdateSuccess cmd = NetUtils.bytesToStruct<CMD_MB_UpdateSuccess>(mainMsg);
        }
        else if (command.wMainCmdID == Protocal.SUB_MB_UPDATE_FAILURE)
        {
            GameUtilty.Info("升级失败");
            CMD_MB_LogonFailure cmd = NetUtils.bytesToStruct<CMD_MB_LogonFailure>(mainMsg);
        }
    }


    /// <summary>
    /// MDM_GP_LOGON_HANDLER
    /// </summary>
    private void MDM_GP_LOGON_HANDLER(TCP_Command command, byte[] mainMsg)
    {
    }


    /// <summary>
    /// MDM_MB_SERVER_LIST_HANDLER
    /// </summary>
    private void MDM_MB_SERVER_LIST_HANDLER(TCP_Command command, byte[] mainMsg)
    {
        if (command.wSubCmdID == Protocal.SUB_MB_LIST_FINISH)
        {
            GameUtilty.Info("=========== 列表接受完成 ===========");
            //查询玩家信息信息
            CMD_GP_QueryIndividual cm;
            cm.dwUserID = SingletonInstance<PlayerInfo>.Instance().dwUserID;
            cm.szPassword = SingletonInstance<PlayerInfo>.Instance().szDynamicPass;
            LogonClient.Instance.SendData(cm, Protocal.MDM_GP_USER_SERVICE, Protocal.SUB_GP_QUERY_INDIVIDUAL);
        }
        else if (command.wSubCmdID == Protocal.SUB_MB_LIST_SERVER)
        {
            List<CMD_CS_C_RegisterServer> list = new List<CMD_CS_C_RegisterServer>();
            int size = Marshal.SizeOf(typeof(CMD_CS_C_RegisterServer));
            GameUtilty.Info("****************收到101房间列表***********************{0},{1}", size, mainMsg.Length);
            int index = 0;
            if (mainMsg.Length % size == 0)
            {
                while (index < mainMsg.Length)
                {
                    byte[] byteCopy = new byte[size];
                    Array.Copy(mainMsg, index, byteCopy, 0, size);
                    index = index + size;
                    CMD_CS_C_RegisterServer cmd = NetUtils.bytesToStruct<CMD_CS_C_RegisterServer>(byteCopy);
                    SingletonInstance<GameInfo>.Instance().AddRoomDataInDic(cmd);
                }
            }
            else
            {
                //未被整除
                GameUtilty.Error("长度有问题total = {0},size = {1}", mainMsg.Length, size);
            }
        }
        else if (command.wSubCmdID == Protocal.SUB_MB_LIST_MATCH)
        {
            GameUtilty.Info("=========== 比赛数据 ===========");
        }
        else if (command.wSubCmdID == Protocal.SUB_MB_SERVER_TAG_RES)
        {
            GameUtilty.Info("****************收到百人房间数据***********************");
            CMD_MB_ServerTagRes cmd = NetUtils.bytesToStruct<CMD_MB_ServerTagRes>(mainMsg);
            GameUtilty.Info("****************收到百人房间数据***********************");
        }
        else if (command.wSubCmdID == Protocal.SUB_MB_LIST_U3D_GAME)
        {
            // GameUtilty.Info("收到byte长度数{0}",mainMsg.Length);
            // GameUtilty.Info("login收到包的消息号 command.wMainCmdID,command.wSubCmdID========={0},{1}",command.wMainCmdID,command.wSubCmdID);
            // GameUtilty.Error("===================================================================|||||||");
            int size = Marshal.SizeOf(typeof(tagU3DGameKind));
            int count = 0;
            
            if (mainMsg.Length % size == 0)
            {
                int packageCount = mainMsg.Length / size;
                GameUtilty.Info("packageCount ====== {0}",packageCount);
                ByteBuffer buffer = new ByteBuffer(mainMsg);
                while (buffer.getLength() != buffer.getCurReadLength())
                {
                    tagU3DGameKind cmd  = buffer.ReadObject<tagU3DGameKind>();
                    GameUtilty.Info("cmd = {0}",cmd.szKindName);
                    SingletonInstance<GameInfo>.Instance().AddGameDataInDic(cmd);
                    count++;
                }
            }
            else
            {
                //未被整除
                GameUtilty.Error("长度有问题total = {0},size = {1}", mainMsg.Length, size);
            }
            
            GameUtilty.Info("收到游戏列表============");
        }
        else if (command.wSubCmdID == Protocal.SUB_MB_LIST_GAME_END)
        {
            GameUtilty.Info("游戏列表接收完成 游戏数量{0} ", SingletonInstance<GameInfo>.Instance().GetGameDicSize());
        }
        else if (command.wSubCmdID == Protocal.SUB_MB_AGENT_KIND)
        {
            // GameUtilty.Info("游戏列表接收完成 游戏数量");
        }
        else
        {
            GameUtilty.Error("未定义消息号{0}", command.wSubCmdID);
        }
    }

    /// <summary>
    /// MDM_GP_SERVER_LIST_HANDLER
    /// </summary>
    /// <param name="command"></param>
    /// <param name="buffer"></param>
    private void MDM_GP_SERVER_LIST_HANDLER(TCP_Command command, byte[] mainMsg)
    {
        if (command.wSubCmdID == Protocal.SUB_GP_LIST_PROPERTY)
        {
            int size = Marshal.SizeOf(typeof(tagPropertyItem));
            int count = mainMsg.Length / size;
            GameUtilty.Info("道具列表数量{0},{1},{2}", count,size,mainMsg.Length );
            ByteBuffer buffer = new ByteBuffer(mainMsg);
            while (count > 0)
            {
                tagPropertyItem cmd = buffer.ReadObject<tagPropertyItem>();
                if (cmd.dwPropertyID == 306)
                {
                    if (cmd.cbExchangeType == 0)
                    {
                    }
                    else if (cmd.cbExchangeType == 1)
                    {
                        SingletonInstance<PlayerInfo>.Instance().CoinBuyPropCount = cmd.dwExchangeRatio;
                    }
                }

                count--;
            }

            if (buffer != null)
            {
                buffer.Close();
            }

            return;
        }
    }
}